<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<script type="text/javascript">
var orgSetting = {	
		view: {
			nameIsHTML: true,
			fontCss: getFont//,
			//selectedMulti : true
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		
		check : {
			enable : true,
			chkboxType : { "Y" : "", "N" : "" }
		},
		callback: {
			onDblClick: zOrgTreeOnDblClick
		}
};
function getFont(treeId, node) {
	if(node.searchHit == true){
		return {'color':'#f79263'};
	}else if(node.ifdel == '8'){
		return {'color':'#999'};
	}else if(node.ifdel == '1'){
		return {'color':'#000'};
	}else{
		return {};
	}
}
var orgTreeObj;
var zOrgNodes =${zOrgNodes};
$(function(){
	orgTreeObj = $.fn.zTree.init($("#zOrgTree"), orgSetting, zOrgNodes);
});
function count() {
	var checkedNodes = orgTreeObj.getCheckedNodes(true);
	var arrayIds = new Array();
	var arrayNames = new Array();
	for ( var i in checkedNodes) {
		arrayIds.push(checkedNodes[i].id);
		arrayNames.push(checkedNodes[i].name);
	}
	$("#orgCodes").val(arrayIds.join(","));
	$("#orgNames").val(arrayNames.join(","));
	$("#hasSelectedOrgNums").html("已选择"+arrayIds.length+"个机构！");
}
/*
 * 获取指定树treeObj的不需要隐藏的节点
 */
 function pushParentNode(nodes,node,searchStr){
		if(node != null && node.name.indexOf(searchStr) == -1){
			nodes.push(node);
			node = node.getParentNode();
			pushParentNode(nodes,node,searchStr);
		}
	}

/*
 * 搜索指定树treeObj中节点名称"name"中包含指定字符串searchStr的节点和其父节点，将其他节点隐藏
 */
function searchFromTree(treeObj,searchStr){
	var nodes = treeObj.getNodes();
	nodes = treeObj.transformToArray(nodes);
	treeObj.hideNodes(nodes);
	var nodes = treeObj.getNodesByParamFuzzy("name", searchStr, null);
	if (nodes.length > 0) {
		for (var int = 0; int < nodes.length; int++) {
			var node = nodes[int].getParentNode();
			pushParentNode(nodes,node,searchStr);
		}
	}
	treeObj.showNodes(nodes);
}

/*
 * 搜索没有管理权限的单位树otherTree中节点名称"name"中包含指定字符串searchStr的节点和其父节点，将其他节点隐藏
 */
function searchOrgTree(searchStr){
	var treeObj = $.fn.zTree.getZTreeObj("zOrgTree");
	searchFromTree(treeObj,searchStr);
	treeObj.expandAll(true);
}

/**
 * 双击非根节点选择单位
 */
function zOrgTreeOnDblClick(event, treeId, treeNode) {
	if(treeNode.level == 0){
	}else{
		if(isOrgSelected()){
			selectedOrg();
		}
	}
};

function checkAndSelectedOrg() {
	if(isOrgSelected()){
		count();
		selectedOrg();
	}
};

/**
 * 检查当前节点是否有选中
 */
function isOrgSelected(){
	var isSelected = false;
	var orgs = orgTreeObj.getCheckedNodes(true);
	if(orgs.length >0){
		selected = true;
		return selected;
	}else{
		ui.msg( '请选择单位！',0 );
		return false;
	}
}

/**
 * 检查当前节点是否有选中
 */
function getCurrentOrgNode(){
	var orgs = orgTreeObj.getSelectedNodes();
	if(isOrgSelected()){
		return orgs[0];
	}
}

</script>
 <div class="row">
	<div class="col-xs-12">
		<div class="widget-box">
			<div class="widget-body">
				<div class="widget-main">
					<div class="row">
						<div class="col-xs-12">
							<div class="input-group" style="width: 100%">
								<span class="input-icon" style="width: 100%">
									<input type="text" class="form-control" id="form-field-icon-1" placeholder="按组织机构名搜索" oninput="searchOrgTree(this.value);" onpropertychange="searchOrgTree(this.value);"/>
									<i class="icon-search purple"></i>
								</span>
							</div>
						</div>
					</div>
					<div class="row mg-top12" style="overflow:auto; height: 400px">
						<div id="zOrgTree" class="ztree"></div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div> 

<div class="modal-footer wizard-actions">
	<button class="btn btn-sm btn-info" onclick="checkAndSelectedOrg();"
		data-last="Finish ">
		<i class="icon-ok bigger-110"></i>提交
	</button>
	<button class="btn btn-danger btn-sm pull-left" data-dismiss="modal">
		<i class="icon-remove"></i> 关闭
	</button>
</div>
